﻿using lwzlz.Application.System;
using lwzlz.Web.Areas.SysAdmin.Models;
using lzwlz.Web;
using System;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.Mall.Controllers
{
    public class OrderController : MallController
    {
        #region 界面
        public ActionResult List()
        {
            return View();
        }
        #endregion

        #region 接口
        /// <summary>
        /// 订单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult OrderList(int pageNumber, int pageSize, string keyword = null)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var orderService = new OrderService();
                var memberId = MallMemberLogin.GetCurrentUser().Id;
                var list = orderService.GetList(pageNumber, pageSize, memberId, keyword, out int count);

                response.Data = new { List = list, Count = count, };
                response.Success = true;
                response.Message = "获取成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        /// <summary>
        /// 取消订单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CancelOrder(string id)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var orderService = new OrderService();
                var model = orderService.Get(int.Parse(id));
                if (model != null)
                {
                    if (model.Status == 10)
                    {
                        model.Status = 90;
                        model.LastModificationTime = DateTime.Now;
                        var result = orderService.Update(model, null);
                        if (result.HasValue && result.Value > 0)
                        {
                            var orderDetailList = new OrderDetailService().GetList(model.Id);
                            foreach (var item in orderDetailList)
                            {
                                var commodity = new CommodityService().Get(item.CommodityId);
                                if (commodity != null && !commodity.IsDeleted)
                                {
                                    commodity.Number += item.Number.Value;
                                    new CommodityService().Update(commodity, null);
                                }
                            }

                            response.Success = true;
                            response.Message = "取消成功";
                        }
                        else
                        {
                            throw new Exception("取消错误");
                        }
                    }
                    else
                    {
                        throw new Exception("订单无法取消");
                    }
                }
                else
                {
                    throw new Exception("订单错误");
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        /// <summary>
        /// 删除订单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DeleteOrder(string id)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var orderService = new OrderService();
                orderService.DeleteList(id.ToString());
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
        #endregion
    }
}